package com.bdqn.dao;

import com.bdqn.entity.Stu;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * <h3>项目名称:demo02222</h3>
 * <p>本API描述:操作学生表数据的类</p>
 * <p>@author : AliyZhang</p>
 * <p>@date : 2021-02-22 17:09</p>
 **/
public class StuDaoImpl extends BaseDao{


    //查询数据
    public List<Stu> show(){
        String sql="select * from stu order by sid";
        ResultSet rs = executeQuery(sql, null);
        List<Stu> list=new ArrayList<>();
        try{
            while(rs.next()){
                int sid=rs.getInt(1);
                String sname=rs.getString(2);
                String spwd=rs.getString(3);
                String gender=rs.getString(4);
                int gid=rs.getInt(5);
                list.add(new Stu(sid,sname,spwd,gender,gid));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //增加数据
    public int insert(Stu stu){
        int rows=0;
        String sql="insert into stu(sname,spwd,gender,gid) values(?,?,?,?) ";
        Object[] params={stu.getSname(),stu.getSpwd(),stu.getGender(),stu.getGid()};
        rows=executeUpdate(sql,params);
        if (rows>0){
            System.out.println("数据增加成功！");
        }else{
            System.out.println("数据增加失败！");
        }

        return rows;
    }

    //修改数据
    public int update(Stu stu){
        int rows=0;
        String sql="update stu set sname=?,spwd=?,gender=?,gid=? where sid=?";
        Object[] params={stu.getSname(),stu.getSpwd(),stu.getGender(),stu.getGid(),stu.getSid()};
        rows=executeUpdate(sql,params);
        if (rows>0){
            System.out.println("数据修改成功！");
        }else{
            System.out.println("数据修改失败！");
        }
        return rows;
    }

    //删除数据
    public int delete(int sid){
        int rows=0;
        String sql="delete from stu where sid=?";
        Object[] params={sid};
        rows=executeUpdate(sql,params);
        if (rows>0){
            System.out.println("数据删除成功！");
        }else{
            System.out.println("数据删除失败！");
        }
        return rows;
    }

}
